package cn.iocoder.yudao.module.shop.service.refundcardrecord;

import cn.iocoder.yudao.framework.mybatis.core.query.LambdaQueryWrapperX;
import cn.iocoder.yudao.module.member.api.user.dto.MemberUserRespDTO;
import cn.iocoder.yudao.module.pay.dal.dataobject.wallet.PayWalletDO;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;

import java.util.*;
import cn.iocoder.yudao.module.shop.controller.admin.refundcardrecord.vo.*;
import cn.iocoder.yudao.module.shop.dal.dataobject.refundcardrecord.ShopRefundCardRecordDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;

import cn.iocoder.yudao.module.shop.convert.refundcardrecord.ShopRefundCardRecordConvert;
import cn.iocoder.yudao.module.shop.dal.mysql.refundcardrecord.ShopRefundCardRecordMapper;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;
import static cn.iocoder.yudao.module.shop.enums.ErrorCodeConstants.*;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.collection.ListUtil;

/**
 * 退卡记录 Service 实现类
 *
 * @author 曹建伟
 */
@Service
@Validated
public class ShopRefundCardRecordServiceImpl implements ShopRefundCardRecordService {

    @Resource
    private ShopRefundCardRecordMapper refundCardRecordMapper;

    @Override
    public Long createRefundCardRecord(ShopRefundCardRecordCreateReqVO createReqVO) {
        // 插入
        ShopRefundCardRecordDO refundCardRecord = ShopRefundCardRecordConvert.INSTANCE.convert(createReqVO);
        refundCardRecordMapper.insert(refundCardRecord);
        // 返回
        return refundCardRecord.getId();
    }

    @Override
    public Long createRefundCardRecord(MemberUserRespDTO member, PayWalletDO wallet, String comment, Integer refundBalance){
        ShopRefundCardRecordDO record = new ShopRefundCardRecordDO();
        record.setComment(comment);
        record.setRefundBalance(wallet.getBalance());
        record.setWalletId(wallet.getId());
        record.setMemberId(member.getId());
        record.setMemberName(member.getNickname());
        record.setRefundBalance(refundBalance*100);
        record.setLeftBalance(wallet.getBalance());

        refundCardRecordMapper.insert(record);

        return record.getId();
    }

    @Override
    public void updateRefundCardRecord(ShopRefundCardRecordUpdateReqVO updateReqVO) {
        // 校验存在
        validateRefundCardRecordExists(updateReqVO.getId());
        // 更新
        ShopRefundCardRecordDO updateObj = ShopRefundCardRecordConvert.INSTANCE.convert(updateReqVO);
        refundCardRecordMapper.updateById(updateObj);
    }

    @Override
    public void deleteRefundCardRecord(Long id) {
        // 校验存在
        validateRefundCardRecordExists(id);
        // 删除
        refundCardRecordMapper.deleteById(id);
    }

    private void validateRefundCardRecordExists(Long id) {
        if (refundCardRecordMapper.selectById(id) == null) {
            throw exception(REFUND_CARD_RECORD_NOT_EXISTS);
        }
    }

    @Override
    public ShopRefundCardRecordDO getRefundCardRecord(Long id) {
        return refundCardRecordMapper.selectById(id);
    }

    @Override
    public List<ShopRefundCardRecordDO> getMemberRefundCardRecord(Long memberId){
        return refundCardRecordMapper.selectList(new LambdaQueryWrapperX<ShopRefundCardRecordDO>()
                .eq(ShopRefundCardRecordDO::getMemberId, memberId));
    }

    @Override
    public List<ShopRefundCardRecordDO> getRefundCardRecordList(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return ListUtil.empty();
        }
        return refundCardRecordMapper.selectBatchIds(ids);
    }

    @Override
    public PageResult<ShopRefundCardRecordDO> getRefundCardRecordPage(ShopRefundCardRecordPageReqVO pageReqVO) {
        return refundCardRecordMapper.selectPage(pageReqVO);
    }

    @Override
    public List<ShopRefundCardRecordDO> getRefundCardRecordList(ShopRefundCardRecordExportReqVO exportReqVO) {
        return refundCardRecordMapper.selectList(exportReqVO);
    }

}
